package com.xf.protocol;

import com.xf.common.Invocation;
import com.xf.common.Serviceinfo;

import java.io.*;
import java.net.*;

public class HttpClient {
    public Serviceinfo req_for_service(String hostname, Integer port) {
        // 连接到服务器
        Socket socket = null;
        try {
            socket = new Socket(hostname,port);
        } catch (IOException e) {
            System.out.println("连接注册中心失败！");
        }
        ObjectOutputStream out=null;
        try {
            out=new ObjectOutputStream(socket.getOutputStream());
            out.writeObject("message");
        } catch (IOException e) {
            System.out.println("发送请求失败！");
        }
        // 创建输出输入流
        ObjectInputStream in = null;
        Serviceinfo serviceinfo = null;
        try {
            in = new ObjectInputStream(socket.getInputStream());
            serviceinfo = (Serviceinfo) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("读取服务信息时失败！");
        }

        // 关闭输出输入流和套接字
        try {
            in.close();
            socket.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return serviceinfo;
    }
    public void send_rig(String hostname, Integer hostport, Invocation invocation,String serAddress,Integer serport){
        // 连接到服务器
        Socket socket = null;
        try {
            socket = new Socket(hostname, hostport);
        } catch (IOException e) {
            System.out.println("与注册中心连接异常！");
        }
        // 创建输出输入流
        ObjectOutputStream out = null;
        // 发送对象到服务器
        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(invocation);
            out.writeObject(serAddress);
            out.writeObject(serport);
        } catch (IOException e) {
            System.out.println("发送注册信息时异常");
        }
        // 关闭输出流和套接字
        try {
            out.close();
            socket.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public Object cfs_con_rig(String hostname,Integer port,Invocation invocation) {
        Socket socket = null;
        try {
            socket = new Socket(hostname,port);
        } catch (IOException e) {
            System.out.println("连接注册中心失败！");
        }
        try {
            socket.setSoTimeout(100000);
        } catch (SocketException e) {
            System.out.println("等待服务结果超时！");
        }
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(invocation);
        } catch (IOException e) {
            System.out.println("发送请求失败！");
        }
        Object result = null;
        try {
            in = new ObjectInputStream(socket.getInputStream());
            result = (Object)in.readObject();
            return result;
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("接收返回结果失败");
            return "接收返回结果失败";
        }
    }


    public Object cfs_rig_pro(String hostname,Integer port,Invocation invocation) {
        Socket socket = null;
        try {
            socket = new Socket(hostname,port);
        } catch (IOException e) {
            System.out.println("连接服务端失败！");
        }
        try {
            socket.setSoTimeout(100000);
        } catch (SocketException e) {
            System.out.println("等待服务结果超时！");
        }
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(invocation);
        } catch (IOException e) {
            System.out.println("发送请求失败！");
        }
        Object result = null;
        try {
            in = new ObjectInputStream(socket.getInputStream());
            result = (Object)in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("接收返回结果失败");
        }
        return result;
    }
}
